﻿using MetaVideoEditorEvolved.Engine;
using MetaVideoEditorEvolved.Engine.Entity;
using MetaVideoEditorEvolved.Messages;

namespace MetaVideoEditorEvolved.ViewModel
{
	using System;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.ComponentModel;
	using System.Linq;
	using System.Windows;
	using System.Windows.Forms;
	using System.Windows.Input;
	using GalaSoft.MvvmLight;
	using GalaSoft.MvvmLight.Command;
	using GalaSoft.MvvmLight.Messaging;
	using mveEngine;

	public class ConfigWizardVM : ViewModelBase
	{
		private int tabIndex;

		private bool 	useMediaBrowser,
		useDvdId,
		useXbmc;

		private string folderSelected;

		private ObservableCollection<AddInInformation> addInProvider;
		
		private ObservableCollection<string> folders = new ObservableCollection<string>(),
		mediaBrowserFolders;

		public ConfigWizardVM()
		{
			addInProvider = new ObservableCollection<AddInInformation>(AddInProviderManager.Current.AddIns);
			
			MediaBrowserFolders = new ObservableCollection<string>(FileUtil.GetMBfolders());

			InitializeCommand();
		}
		
		private void InitializeCommand()
		{
			PreviousCommand = new RelayCommand(() =>
			                                   {
			                                   	TabPageIndex--;
			                                   },
			                                   () =>
			                                   {
			                                   	return TabPageIndex != 0;
			                                   });

			NextCommand = new RelayCommand(() =>
			                               {
			                               	TabPageIndex++;
			                               },
			                               () =>
			                               {
			                               	return TabPageIndex != 2;
			                               });

			FinishCommand = new RelayCommand(() =>
			                                 {
			                                 	Config.Instance.RootFolders = Folders.ToList();

			                                 	
			                                 	foreach (var addIn in AddInSaverManager.Current.AddIns) {
			                                 		addIn.IsEnable = false;
			                                 		
			                                 		if(UseMediaBrowser && 
			                                 		   addIn.Name.ToLower().Contains("mediabrowser"))
		                                 			{
		                                 				addIn.IsEnable = true;
			                                 		}
			                                 		
			                                 		if(UseXbmc &&
			                                 		   addIn.Name.ToLower().Contains("xbmc"))
			                                 		{
			                                 			addIn.IsEnable = true;
			                                 		}
			                                 		
			                                 		if(UseDvdId &&
			                                 		   addIn.Name.ToLower().Contains("dvd"))
			                                 		{
			                                 			addIn.IsEnable = true;
			                                 		}
			                                 	}
			                                 	
			                                 	foreach (var addIn in AddInReaderManager.Current.AddIns) {
			                                 		addIn.IsEnable = false;
			                                 		
			                                 		if(UseMediaBrowser && 
			                                 		   addIn.Name.ToLower().Contains("mediabrowser"))
		                                 			{
		                                 				addIn.IsEnable = true;
			                                 		}
			                                 		
			                                 		if(UseXbmc &&
			                                 		   addIn.Name.ToLower().Contains("xbmc"))
			                                 		{
			                                 			addIn.IsEnable = true;
			                                 		}
			                                 		
			                                 		if(UseDvdId &&
			                                 		   addIn.Name.ToLower().Contains("dvd"))
			                                 		{
			                                 			addIn.IsEnable = true;
			                                 		}
			                                 	}
			                                 	
			                                 	AddInProviderManager.Current.SaveAllConfiguration();
			                                 	AddInReaderManager.Current.SaveAllConfiguration();
			                                 	AddInSaverManager.Current.SaveAllConfiguration();
			                                 	
			                                 	Config.Instance.IsFirstRun = false;
			                                 	
			                                 	Messenger.Default.Send<CloseMessage>(new CloseMessage { WindowToClose = WindowType.ConfigWizard });
			                                 });

			AddFolderCommand = new RelayCommand(() =>
			                                    {
			                                    	FolderBrowserDialog folderBrowser = new FolderBrowserDialog();

			                                    	if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
			                                    	    !Folders.Contains(folderBrowser.SelectedPath))
			                                    	{
			                                    		Folders.Add(folderBrowser.SelectedPath);
			                                    	}
			                                    });

			RemoveFolderCommand = new RelayCommand(() =>
			                                       {
			                                       	Folders.Remove(FolderSelected);
			                                       },
			                                       () =>
			                                       {
			                                       	return !string.IsNullOrEmpty(FolderSelected);
			                                       });

			MediaBrowserFolderCommand = new RelayCommand(() =>
			                                             {
			                                             	foreach (var item in MediaBrowserFolders)
			                                             	{
			                                             		if (!Folders.Contains(item))
			                                             		{
			                                             			Folders.Add(item);
			                                             		}
			                                             	}
			                                             });
		}

		public ICommand PreviousCommand { get; private set; }

		public ICommand NextCommand { get; private set; }

		public ICommand FinishCommand { get; private set; }

		public ICommand AddFolderCommand { get; private set; }

		public ICommand RemoveFolderCommand { get; private set; }

		public ICommand MediaBrowserFolderCommand { get; private set; }

		public int TabPageIndex
		{
			get
			{
				return tabIndex;
			}

			set
			{
				tabIndex = value;
				RaisePropertyChanged("TabPageIndex");
			}
		}

		public bool UseMediaBrowser
		{
			get
			{
				return useMediaBrowser;
			}

			set
			{
				useMediaBrowser = value;
				RaisePropertyChanged("UseMediaBrowser");
			}
		}

		public bool UseDvdId
		{
			get
			{
				return useDvdId;
			}

			set
			{
				useDvdId = value;
				RaisePropertyChanged("UseDvdId");
			}
		}

		public bool UseXbmc
		{
			get
			{
				return useXbmc;
			}

			set
			{
				useXbmc = value;
				RaisePropertyChanged("UseXbmc");
			}
		}

		public ObservableCollection<string> Folders
		{
			get
			{
				return folders;
			}

			set
			{
				folders = value;
				RaisePropertyChanged("Folders");
			}
		}

		public ObservableCollection<string> MediaBrowserFolders
		{
			get
			{
				return mediaBrowserFolders;
			}

			set
			{
				mediaBrowserFolders = value;
				RaisePropertyChanged("MediaBrowserFolders");
			}
		}

		public string FolderSelected
		{
			get
			{
				return folderSelected;
			}

			set
			{
				folderSelected = value;
				RaisePropertyChanged("FolderSelected");
			}
		}

		public ObservableCollection<AddInInformation> ProviderAddIn
		{
			get
			{
				return addInProvider;
			}
			
			set
			{
				addInProvider = value;
				RaisePropertyChanged("ProviderAddIn");
			}
		}
	}
}
